package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.ShopingCar;
import cn.wolfcode.trip.base.mapper.ShopingCarMapper;
import cn.wolfcode.trip.base.query.ShopingCarQueryObject;
import cn.wolfcode.trip.base.service.IShopingCarService;
import cn.wolfcode.trip.base.util.UserContext;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ShopingCarServiceImpl implements IShopingCarService {
    @Autowired
    private ShopingCarMapper shopingCarMapper;

    @Override
    public PageInfo query(ShopingCarQueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
        List<ShopingCar> list = shopingCarMapper.selectForList(qo);
        return new PageInfo(list);
    }


    @Override
    public void saveOrUpdate(ShopingCar car) {
        ShopingCar shopingCar = shopingCarMapper.selectShopingCar(car);
        if(shopingCar!=null){
            Integer number = shopingCar.getNumber()+car.getNumber();
            car.setNumber(number);
            shopingCarMapper.updateRelation(car);
        }else{
            shopingCarMapper.insertRelation(car);
        }
    }

    @Override
    public void delete(Long productId) {
        Long userId = UserContext.getCurrentUser().getId();
        shopingCarMapper.deleteByProductId(userId,productId);
    }

    @Override
    public void updateNum(ShopingCar shopingCar) {
        shopingCarMapper.updateRelation(shopingCar);
    }

}
